package research.toe.duangjarern;


import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.internal.view.menu.ListMenuItemView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import research.toe.duangjarern.Helper.ListAdapter;


public class DailyFragment extends Fragment {


    public DailyFragment() {
        // Required empty public constructor
    }

    ListView list;
    ListAdapter adapter;
    Button btnAdd ,btnSearch;
    DatePickerDialog Datepicker;

    int mYear,mMonth,mDay; //for default
    int nYear,nMonth,nDay;
    int tYear,tMonth,tDay;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView  = inflater.inflate(R.layout.fragment_daily, container, false);

            ArrayList<HashMap<String, String>> ArrList = new ArrayList<HashMap<String, String>>();//dummy

            HashMap map = new HashMap<String, String>();//dummy
            map.put("Time","10:30:97  12/10/2557");//dummy

            btnAdd = (Button)rootView.findViewById(R.id.btnAdd);
            btnSearch = (Button)rootView.findViewById(R.id.btnSearch);
            Calendar newCalendar = Calendar.getInstance();

            ArrList.add(map); //dummy

            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

            list = (ListView)rootView.findViewById(R.id.lvQuestionair);

            adapter = new ListAdapter(getActivity(), ArrList);
            list.setAdapter(adapter);

            Datepicker = new DatePickerDialog(getActivity(),new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    nYear=year;
                    nMonth=monthOfYear;
                    nDay=dayOfMonth;
                }
            },mYear, mMonth, mDay);



            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    datePrepare();

                    Datepicker.setTitle("กรุณาเลือกวันที่จะทำแบบสำรวจ");
                    Datepicker.show();
//                    Intent intent = new Intent(getActivity(), QuestionnaireActivity.class);
//                    intent.putExtra("QType", "Daily");
//                    intent.putExtra("Year",nYear);
//                    intent.putExtra("Month",nMonth);
//                    intent.putExtra("Day",nDay);
//                    startActivity(intent);
                    //putExtra => nYear, nMonth, nDay
                    //new intent to QuestionnaireActivity
                    //puExtra => Daily

                }

            });
        Datepicker.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {

                if(checkSetDate()) {
                    Intent intent = new Intent(getActivity(), QuestionnaireActivity.class);
                    intent.putExtra("QType", "Daily");
                    intent.putExtra("Year", nYear);
                    intent.putExtra("Month", nMonth);
                    intent.putExtra("Day", nDay);
                    startActivity(intent);
                }
                else {
                    return;
                }
            }
        });


            btnSearch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Datepicker.show();
                    //send data to server
                    //get json result
                    //display and addto array and show
                }
            });

            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getActivity(), QuestionnaireActivity.class);
                    intent.putExtra("QType", "Daily");
                    startActivity(intent);
                }
            });


        return rootView;
    }

    public void datePrepare(){
        nYear=0;
        nMonth=0;
        nDay=0;
    }

    public boolean checkSetDate(){
        return (nYear!=0)&&(nMonth!=0)&&(nDay!=0);
    }


}
